package com.abo.yaas.view.widgets;

import java.util.Locale;

import com.abo.yaas.authentication.SessionHandler;
import com.abo.yaas.authentication.UserChangeEvent;
import com.abo.yaas.authentication.UserChangeListener;
import com.abo.yaas.domain.User;
import com.abo.yaas.i18n.Lang;
import com.abo.yaas.i18n.LocaleChangeListener;
import com.abo.yaas.i18n.LocaleChangedEvent;
import com.abo.yaas.view.ViewAction;
import com.abo.yaas.view.ViewHandler;
import com.abo.yaas.view.widgets.fields.LocaleCombo;
import com.abo.yaas.view.widgets.panels.AuctionFormPanel;
import com.abo.yaas.view.widgets.panels.AuctionTablePanel;
import com.abo.yaas.view.widgets.panels.UserFormPanel;
import com.abo.yaas.webservice.ResourceProvider;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.Runo;

@SuppressWarnings("serial")
public class HeaderView extends HorizontalLayout implements Button.ClickListener, UserChangeListener,
        LocaleChangeListener {
    private final Button newAuctionButton = new LocaleSensitiveButton("CREATE_AUCTION_BUTTON", this, "showAuctionForm");

    private final Button auctionListButton = new LocaleSensitiveButton("AUCTIONS_BUTTON", this, "showAuctions");

    private final Button signUpButton = new LocaleSensitiveButton("SIGN_UP_BUTTON", this, "showSignUp");

    private final Button loginButton = new LocaleSensitiveButton("LOGIN_BUTTON", this, "login");

    private final Button logoutButton = new LocaleSensitiveButton("LOGOUT_BUTTON", this, "logout");

    private final Button settingsButton = new LocaleSensitiveButton("MY_SETTINGS_BUTTON", this, "editSettings");

    private final PasswordField passwordField = new PasswordField();

    private final TextField loginField = new TextField();

    private final Label loginLabel = new LocaleSensitiveLabel("LOGIN_LABEL");

    private final Label pwdLabel = new LocaleSensitiveLabel("PASSWORD_LABEL");

    private final Label userNameLabel = new LocaleSensitiveLabel("LOGGED_AS_MSG");

    private final Label logoLabel = new Label("<b>YAAS</b>", Label.CONTENT_XHTML) {
        {
            setSizeUndefined();
            addStyleName("v-logo");
        }
    };

    public HeaderView() {
        super();
        addStyleName("v-header-view");
        setSizeUndefined();
        setWidth("100%");
        setMargin(true);
        setSpacing(true);

        loginField.setSizeUndefined();
        loginField.addStyleName(Runo.TEXTFIELD_SMALL);
        passwordField.addStyleName(Runo.TEXTFIELD_SMALL);

        signUpButton.addStyleName(BaseTheme.BUTTON_LINK);
        loginButton.addStyleName(BaseTheme.BUTTON_LINK);
        logoutButton.addStyleName(BaseTheme.BUTTON_LINK);

        newAuctionButton.addStyleName(Runo.BUTTON_SMALL);
        settingsButton.addStyleName(Runo.BUTTON_SMALL);
        auctionListButton.addStyleName(Runo.BUTTON_SMALL);

        setSpacing(true);

        buildView();
    }

    private void buildLoginLayout() {
        removeAllComponents();
        final User user = SessionHandler.getUser();
        final LocaleCombo combo = new LocaleCombo();
        combo.addListener(new ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                final Object value = combo.getValue();
                final Locale locale = new Locale(String.valueOf(value));
                if (!locale.equals(Lang.getLocale())) {
                    Lang.setLocale(locale);
                }
            }
        });

        addComponent(logoLabel);
        addComponent(auctionListButton);
        if (SessionHandler.getUser() != null) {
            addComponent(newAuctionButton);
            addComponent(settingsButton);
        }

        if (user == null) {
            addComponent(loginLabel);
            addComponent(loginField);
            addComponent(pwdLabel);
            addComponent(passwordField);
            addComponent(combo);
            addComponent(loginButton);
            addComponent(signUpButton);
            setExpandRatio(loginLabel, 1);
        } else {
            userNameLabel.setValue(user.getFullName());
            addComponent(userNameLabel);
            addComponent(combo);
            addComponent(logoutButton);
            setExpandRatio(userNameLabel, 1);
        }
    }

    @Override
    public void addComponent(Component c) {
        super.addComponent(c);
        setComponentAlignment(c, Alignment.MIDDLE_RIGHT);
    }

    private void buildView() {
        buildLoginLayout();
    }

    @Override
    public void buttonClick(ClickEvent event) {
    }

    public void editSettings() {
        ViewHandler.activateView(UserFormPanel.class,
                new Object[] { ViewAction.VA_EDIT_USER, SessionHandler.getUser() });
    }

    @Override
    public void localeChanged(LocaleChangedEvent event) {

    }

    public void login() {
        final String login = String.valueOf(loginField.getValue());
        final String password = String.valueOf(passwordField.getValue());
        try {
            final User user = ResourceProvider.get().getUserProxy().authenticate(login, password);
            SessionHandler.setUser(user);
        } catch (final UniformInterfaceException e) {
            getWindow().showNotification(e.getResponse().getEntity(String.class));
        }

    }

    public void logout() {
        SessionHandler.setUser(null);
    }

    @Override
    public void onLogin(UserChangeEvent event) {
        buildView();
    }

    @Override
    public void onLogout(UserChangeEvent event) {
        buildView();
    }

    public void showAuctionForm() {
        ViewHandler.activateView(AuctionFormPanel.class, new Object[] {});
    }

    public void showAuctions() {
        ViewHandler.activateView(AuctionTablePanel.class, new Object[] {});
    }

    public void showSignUp() {
        ViewHandler.activateView(UserFormPanel.class, new Object[] { ViewAction.VA_SIGNUP });
    }
}
